pp108 : Understanding XPath Expressions

Understanding XPath Expressions

This topic describes the concept of XPath expressions.


An XPath expression is the first and foremost requirement for implementing XPath. These expressions help you to select information in an XML document. The XPath expression should be syntactically correct, as defined by XPath 1.0 specification.

To understand XPath expression, consider the following XML document as an example:

<PersonList>
    <Person>
        <Name>John</Name>
        <Age>24</Age>
        <Gender>M</Gender>
        <PostalCode>54879</PostalCode>
    </Person>
    <Person>
        <Name>Jasmin</Name>
        <Age>28</Age>
        <Gender>F</Gender>
        <PostalCode>78745</PostalCode>
    </Person>
</PersonList>


To select different types of information from the XML document, different XPath expressions are required. The sample expressions listed below (with reference to the above example) pick different portions of the document, based on the specific requirement.

  • To select all names in the PersonList or Person element, use
    /PersonList/Person/Name

  • To select a specific person with name value 'Jasmine', use
    /PersonList/Person[Name='Jasmin']

    Note: Other than the equal operator ( = ), you can use the greater than ( > ) and less than ( < ), <= (less than or equal to), >= (greater than or equal to) and != (not equal). The logical operators are:
  • and
  • or

  • To search for persons who are older than 18 years old, use
     /PersonList/Person[Age>18]

    Note: Strings are compared case sensitively. You can use '*' as a wildcard. The operators < and > can be used to compare strings alphabetically.
  • To get the second person in the XML document, use
     /PersonList/Person/[2]

    Note: If you want to create a generic expression that can potentially select every person, replace the number '2' with a variable reference. For more information on variable binding, refer to Variable Binding.
  • To get the last node, use
     /PersonList/Person/[last()]

    Note: This is useful for iterations. However, there is no function named first().
  • To select several paths use the ( | ) operator. This will select all name elements and age elements.
     /PersonList/Person/Name | /PersonList/Person/Age

    Note: In NOM, the expression to select the root will be '/node()' and not '/'. However, there is one exception. If you attach the XSLT 'document' function to XPath expression, the document element (first child of root) will be passed to the attached expression. For example, if the expression is :
     document('weather.xml')/child::node()
    and if the document is
    <weather>
        <forecast/>
    </weather>
    
    then, <forecast > will be selected. Therefore, such expressions should be used with caution.

Related reference

Defining XPath Expressions
XPath Parser, XPath Object and XPath Output
Optimizing XPath Expression Usage
Using XPath API

Related information

Overview of XPath